// import dbHelper from '../../../common/dbHelper'
import commonSqlHelper from './commonSqlHelper'
import SQLite from '../../common/SQLiteHelper'

export default {
  /**
   * 获取案件信息
   */
  getCaseInfo: async function (DBFile) {
    try {
      let caseInfos = await SQLite.ExecuteReader({
        SQL: commonSqlHelper.getCaseInfo,
        Parameter: {}
      }, DBFile) || []

      return caseInfos[0]
    } catch (error) {
      throw error
    }
  },
  /**
   * 获取电子卷宗
   */
  getDossierSource: async function (id = '') {
    try {
      let dossier = await SQLite.ExecuteReader({
        SQL: commonSqlHelper.getDossierCatalog(id),
        Parameter: {}
      })

      let ClearRootNode = function (dos, NodeParentID = '') {
        let dossiers = dos.filter((x) => {
          return x.NodeParentID === NodeParentID
        })

        if (dossiers.length === 1 && dossiers[0].FileType === 'dir') {
          let nodeID = dossiers[0].NodeID
          let delIndex = dos.findIndex((a) => {
            return a.NodeID === nodeID
          })

          if (delIndex >= 0) dos.splice(delIndex, 1)

          return ClearRootNode(dos, nodeID)
        } else {
          dossiers.forEach((x) => {
            x.NodeParentID = ''
          })
        }
        return dos
      }

      return ClearRootNode(dossier)
    } catch (error) {
      throw error
    }
  },
  /**
   * 获取文书卷宗
   */
  getDocDossierSource: async function () {
    try {
      let docDossierCatalog = await SQLite.ExecuteReader({
        SQL: commonSqlHelper.getDocDossierCatalog,
        Parameter: {}
      })

      let docDossierFileCatalog = await SQLite.ExecuteReader({
        SQL: commonSqlHelper.getDocDossierFileCatalog,
        Parameter: {}
      })

      docDossierCatalog = docDossierCatalog.concat(docDossierFileCatalog)
      console.log(docDossierCatalog)
      docDossierCatalog.forEach((item) => {
        if (!docDossierCatalog.find((x) => {
          return x.NodeID === item.NodeParentID
        })) {
          item.NodeParentID = ''
        }
      })
      return docDossierCatalog.sort((a, b) => {
        return a.order - b.order
      })
    } catch (error) {
      throw error
    }
  },
  // /**
  //  * 获取卷宗文件
  //  */
  // getDossierFile: async function () {
  //   try {
  //     return await dbHelper.all(commonSqlHelper.getDossierFiles)
  //   } catch (error) {
  //     throw error
  //   }
  // },
  /**
   * 获取文件PDF文件流
   */
  getPDFFileByID: async function (id) {
    let execSql = commonSqlHelper.getFilePDF(id)
    let result = await SQLite.ExecuteReader(execSql)

    return result.length ? result[0].PDF : null
  },
  // 获取检索数据源
  getRetrieveData: async function (type, value) {
    try {
      // return await UtilDLL.SqliteQuery({
      //   path: store.state.appearCourt.casePath,
      //   password: '',
      //   sql: commonSqlHelper.getRetrieveData,
      // });
      return await SQLite.ExecuteReader(commonSqlHelper.getRetrieveData(type, value))

      // return await dbHelper.all(commonSqlHelper.getRetrieveData);
    } catch (error) {
      throw error
    }
  },
  // 获取检索数据源
  getDossierNodeIDs: async function () {
    try {
      return await SQLite.ExecuteReader({
        SQL: commonSqlHelper.getDossierNodeIDs,
        Parameter: {}
      })

      // return await dbHelper.all(commonSqlHelper.getRetrieveData);
    } catch (error) {
      throw error
    }
  },
  getFileIDByCode: async function (arr) {
    try {
      let result = []
      let files = await SQLite.ExecuteReader(commonSqlHelper.getFileIDByCode(arr))
      arr.forEach((item) => {
        result = result.concat(files.filter((x) => {
          return x.WJID === item
        }))
      })

      return result
    } catch (error) {
      throw error
    }
  },
  getFileByCode: async function (arr) {
    try {
      // return await dbHelper.all(commonSqlHelper.getFileByCode(arr));
      // return await UtilDLL.SqliteQuery({
      //   path: store.state.appearCourt.casePath,
      //   password: '',
      //   sql: commonSqlHelper.getFileByCode(arr),
      // });

      return await SQLite.ExecuteReader({
        SQL: commonSqlHelper.getFileByCode(arr),
        Parameter: {}
      })
    } catch (error) {
      throw error
    }
  },
  // getFileByID: async function (id) {
  //   try {
  //     return await dbHelper.get(commonSqlHelper.getFileByID(id))
  //   } catch (error) {
  //     throw error
  //   }
  // },
  getFilesByID: async function (arr) {
    try {
      return await SQLite.ExecuteReader(commonSqlHelper.getFilesByID(arr))
    } catch (error) {
      throw error
    }
  }
  // delDossiers: async function (id) {
  //   try {
  //     return await SQLite.DelEvidenceMaterial({
  //       executeSql: commonSqlHelper.DelDossiers(id)
  //     })
  //   } catch (e) {
  //     throw e
  //   }
  // }
}
